package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    private ClazzMapper clazzMapper;

    public PageResult<Clazz> findAll(String name, LocalDate begin, LocalDate end, Integer page, Integer pageSize) {
        // 开启分页
        PageHelper.startPage(page, pageSize);
        // 查询所有数据, 返回的是一个Page对象

        Page<Clazz> clazzList = (Page<Clazz>) clazzMapper.findAll(name, begin, end);

        PageResult<Clazz> clazzPageResult = new PageResult<>(clazzList.getTotal(), clazzList.getResult());

        return clazzPageResult;
    }

    @Override
    public Clazz queryClazzById(Integer id) {
        Clazz clazz = clazzMapper.queryClazzById(id);
        return clazz;
    }

    @Override
    public void deleteClazzById(Integer id) {
        clazzMapper.deleteClazzById(id);

    }

    @Override
    public void insertClazz(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.insertClazz(clazz);

    }

    @Override
    public void updateClazz(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.updateClazz(clazz);

    }

    @Override
    public List<Clazz> queryAllClazz() {
        List<Clazz> clazzList = clazzMapper.queryAllClazz();
        return clazzList;
    }

}
